package com.zl.learning.thread;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

public class TestConcurrentHashMap {
    public static void main(String[] args) {
        Map<Integer, Integer> map = new ConcurrentHashMap<>();
        IntStream.range(0, 10000).forEach(r -> map.put(r, r));
        new Thread(() -> {
            while (true) {
                map.forEach((k, v) -> System.out.println(k + "--" + v));
                System.out.println("--------------------------------------------------------------------------------------");
            }
        }).start();
        new Thread(() -> {
            try {
                while (true) {
                    TimeUnit.MILLISECONDS.sleep(1);
                    map.keySet().stream().findAny().ifPresent(map::remove);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
